{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "framed-attraction",
   "metadata": {},
   "source": [
    "# Dominican Republic Tax Registrations"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "generic-broad",
   "metadata": {},
   "source": [
    "## Introduction"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "saving-thanksgiving",
   "metadata": {},
   "source": [
    "The function `clean_do_rnc()` cleans a column containing Dominican Republic tax registration (RNC) strings, and standardizes them in a given format. The function `validate_do_rnc()` validates either a single RNC strings, a column of RNC strings or a DataFrame of RNC strings, returning `True` if the value is valid, and `False` otherwise."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "contained-legislature",
   "metadata": {},
   "source": [
    "RNC strings can be converted to the following formats via the `output_format` parameter:\n",
    "\n",
    "* `compact`: only number strings without any seperators or whitespace, like \"131246796\"\n",
    "* `standard`: RNC strings with proper whitespace in the proper places, like \"1-31-24679-6\"\n",
    "\n",
    "Invalid parsing is handled with the `errors` parameter:\n",
    "\n",
    "* `coerce` (default): invalid parsing will be set to NaN\n",
    "* `ignore`: invalid parsing will return the input\n",
    "* `raise`: invalid parsing will raise an exception\n",
    "\n",
    "The following sections demonstrate the functionality of `clean_do_rnc()` and `validate_do_rnc()`. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "terminal-timber",
   "metadata": {},
   "source": [
    "### An example dataset containing RNC strings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "northern-tackle",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "df = pd.DataFrame(\n",
    "    {\n",
    "        \"rnc\": [\n",
    "            \"131246796\",\n",
    "            \"1018A0043\",\n",
    "            \"51824753556\",\n",
    "            \"51 824 753 556\",\n",
    "            \"hello\",\n",
    "            np.nan,\n",
    "            \"NULL\"\n",
    "        ], \n",
    "        \"address\": [\n",
    "            \"123 Pine Ave.\",\n",
    "            \"main st\",\n",
    "            \"1234 west main heights 57033\",\n",
    "            \"apt 1 789 s maple rd manhattan\",\n",
    "            \"robie house, 789 north main street\",\n",
    "            \"(staples center) 1111 S Figueroa St, Los Angeles\",\n",
    "            \"hello\",\n",
    "        ]\n",
    "    }\n",
    ")\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "important-vacuum",
   "metadata": {},
   "source": [
    "## 1. Default `clean_do_rnc`\n",
    "\n",
    "By default, `clean_do_rnc` will clean rnc strings and output them in the standard format with proper separators."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "independent-numbers",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import clean_do_rnc\n",
    "clean_do_rnc(df, column = \"rnc\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eastern-weekend",
   "metadata": {},
   "source": [
    "## 2. Output formats"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "loaded-musician",
   "metadata": {},
   "source": [
    "This section demonstrates the output parameter."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "charitable-killing",
   "metadata": {},
   "source": [
    "### `standard` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "documentary-expansion",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_do_rnc(df, column = \"rnc\", output_format=\"standard\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "wound-surgeon",
   "metadata": {},
   "source": [
    "### `compact`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "honest-coordinator",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_do_rnc(df, column = \"rnc\", output_format=\"compact\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "assigned-electron",
   "metadata": {},
   "source": [
    "## 3. `inplace` parameter\n",
    "\n",
    "This deletes the given column from the returned DataFrame. \n",
    "A new column containing cleaned RNC strings is added with a title in the format `\"{original title}_clean\"`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "saving-server",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_do_rnc(df, column=\"rnc\", inplace=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cordless-break",
   "metadata": {},
   "source": [
    "## 4. `errors` parameter"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "angry-choir",
   "metadata": {},
   "source": [
    "### `coerce` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "professional-draft",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_do_rnc(df, \"rnc\", errors=\"coerce\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "occasional-summer",
   "metadata": {},
   "source": [
    "### `ignore`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "through-construction",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_do_rnc(df, \"rnc\", errors=\"ignore\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "responsible-nigeria",
   "metadata": {},
   "source": [
    "## 4. `validate_do_rnc()`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "separate-offense",
   "metadata": {},
   "source": [
    "`validate_do_rnc()` returns `True` when the input is a valid RNC. Otherwise it returns `False`.\n",
    "\n",
    "The input of `validate_do_rnc()` can be a string, a Pandas DataSeries, a Dask DataSeries, a Pandas DataFrame and a dask DataFrame.\n",
    "\n",
    "When the input is a string, a Pandas DataSeries or a Dask DataSeries, user doesn't need to specify a column name to be validated. \n",
    "\n",
    "When the input is a Pandas DataFrame or a dask DataFrame, user can both specify or not specify a column name to be validated. If user specify the column name, `validate_do_rnc()` only returns the validation result for the specified column. If user doesn't specify the column name, `validate_do_rnc()` returns the validation result for the whole DataFrame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "significant-active",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import validate_do_rnc\n",
    "print(validate_do_rnc(\"131246796\"))\n",
    "print(validate_do_rnc(\"1018A0043\"))\n",
    "print(validate_do_rnc(\"51824753556\"))\n",
    "print(validate_do_rnc(\"51 824 753 556\"))\n",
    "print(validate_do_rnc(\"hello\"))\n",
    "print(validate_do_rnc(np.nan))\n",
    "print(validate_do_rnc(\"NULL\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "stopped-ministry",
   "metadata": {},
   "source": [
    "### Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "capital-language",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_do_rnc(df[\"rnc\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "integral-investigation",
   "metadata": {},
   "source": [
    "### DataFrame + Specify Column"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "southeast-pakistan",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_do_rnc(df, column=\"rnc\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "steady-compound",
   "metadata": {},
   "source": [
    "### Only DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "equipped-julian",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "validate_do_rnc(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "variable-payday",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
